package com.money.tracker.system.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.extension.service.impl.ServiceImpl;
import com.money.tracker.common.enums.RedisKeyEnum;
import com.money.tracker.common.helper.QueryCacheHelper;
import com.money.tracker.system.entity.SysConfig;
import com.money.tracker.system.mapper.SysConfigMapper;
import com.money.tracker.system.service.SysConfigService;
import lombok.RequiredArgsConstructor;
import org.springframework.stereotype.Service;

/**
 * <p>
 * 参数配置表 服务实现类
 * </p>
 *
 * @author zhangshuai
 * @since 2024-09-21
 */
@Service
@RequiredArgsConstructor
public class SysConfigServiceImpl extends ServiceImpl<SysConfigMapper, SysConfig> implements SysConfigService {

    private final QueryCacheHelper queryCacheHelper;

    /**
     * 获取配置信息
     *
     * @param key
     * @return {@link SysConfig }
     */

    @Override
    public SysConfig getSysConfig(String key) {
        String redisKey = RedisKeyEnum.getKey(RedisKeyEnum.SYS_CONFIG_KEY, key);
        return queryCacheHelper.queryWithCache(redisKey, RedisKeyEnum.SYS_CONFIG_KEY.getExpire(),
                RedisKeyEnum.SYS_CONFIG_KEY.getTimeUnit(), () -> getSysConfigDb(key));
    }


    /**
     * 获取配置信息
     *
     * @param key
     * @return
     */
    private SysConfig getSysConfigDb(String key) {
        LambdaQueryWrapper<SysConfig> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(SysConfig::getConfigKey, key);
        return this.getOne(wrapper);
    }
}
